<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-sys__dict">
      <el-form
        :inline="true"
        :model="dataForm"
        @keyup.enter.native="getDataList()"
      >
        <el-form-item>
          <el-button
            type="primary"
            @click="
              type = 2;
              addOrUpdateHandle();
            "
             v-if="$hasPermission('scenic:goods:save')"
            >{{ $t("add") }}</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button type="danger" v-if="$hasPermission('scenic:goods:delete')" @click="deleteHandle(id)">{{
            $t("deleteBatch")
          }}</el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="dataListLoading"
        :data="dataList"
        border
        @selection-change="dataListSelectionChangeHandle"
        @sort-change="dataListSortChangeHandle"
        style="width: 100%"
      >
        <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50"
        ></el-table-column>
        <el-table-column
          prop="goodsName"
          label="商品名称"
          :show-overflow-tooltip="true"
          header-align="center"
          min-width="150"
          align="center"
        ></el-table-column>
        <!-- <el-table-column
          prop="goodsType"
          label="商品-船类型"
          header-align="center"
          min-width="120"
          align="center"
        >
          <template slot-scope="scope">
            {{ scope.row.goodsType | delStatus }}
          </template>
        </el-table-column> -->
        <!-- <el-table-column
          prop="clusteringNum"
          label="成团人数"
          header-align="center"
          min-width="120"
          align="center"
        ></el-table-column> -->
        <el-table-column
          prop="normalPrice"
          label="价格"
          header-align="center"
          min-width="150"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="status"
          label="上/下架状态"
          header-align="center"
          align="center"
          min-width="140px"
        >
          <template slot-scope="scope">
            <el-switch
              @change="(val) => changeStatus(scope.row.id, val)"
              :value="scope.row.status"
              :active-value="0"
              :inactive-value="1"
              active-color="#ddd"
              inactive-color="#13ce66"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
          prop="coverImg"
          label="商品封面"
          min-width="150"
          header-align="center"
          align="center"
        >
          <template slot-scope="scope">
            <el-image
              :src="scope.row.coverImg"
              style="width: 100px; height: 100px"
              v-if="scope.row.coverImg"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('handle')"
          fixed="right"
          header-align="center"
          align="center"
          min-width="200"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              v-if="$hasPermission('scenic:goods:update')"
              @click="edit(scope.row.id, scope.row.goodsType)"
              >编辑</el-button
            >
           
            <el-button
              type="text"
              size="small"
              v-if="$hasPermission('scenic:goods:delete')"
              @click="deleteHandle(scope.row.id)"
              >{{ $t("delete") }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle"
      >
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update
        v-if="addOrUpdateVisible"
        ref="addOrUpdate"
        :goodsType="type"
        @refreshDataList="getDataList"
      ></add-or-update>
    </div>
  </el-card>
</template>

<script>
var that;
import mixinViewModule from "@/mixins/view-module";
import AddOrUpdate from "./goods-add-or-update";
export default {
  mixins: [mixinViewModule],
  filters: {
    delStatus(val) {
      if (val) {
        let d = that.goodsType.filter((item) => item.value == val);
        if (d.length != 0) {
          return d[0].label;
        }
      }
    },
  },
  data() {
    return {
      mixinViewModuleOptions: {
        getDataListURL: "/scenic/goods/page",
        getDataListIsPage: true,
        deleteURL: "/scenic/goods",
        deleteIsBatch: true,
      },
      type: "",
      goodsType: [
        {
          label: "特价",
          value: 1,
        },
        {
          label: "包船",
          value: 2,
        },
        {
          label: "拼船",
          value: 3,
        },
      ],
      dataForm: {
        merchantId: this.$store.state.user.MerchantId|| localStorage.getItem('MerchantId'),
        goodsType: 2,
        delFlag:0
        // name: '',
        // merchantType: '',
        // status: '',
        // activity: '',
        // islandId: '',
        // linkName: '',
        // phone: '',
      },
    };
  },
  components: {
    AddOrUpdate,
  },
  created() {
    that = this;
  },
  methods: {
     changeStatus(id, status) {
            this.$confirm(`确定${status?'上架':'下架'}？`, this.$t("prompt.title"), {
                    confirmButtonText: this.$t("confirm"),
                    cancelButtonText: this.$t("cancel"),
                    type: "warning",
                })
                .then(() => {
                    this.$http
                        .post('/scenic/goods/batchUpDownFrame', [{
                            id,
                            status
                        }])
                        .then((res) => {
                            if (res.data.code !== 0) {
                                return this.$message.error(res.data.msg);
                            }
                            this.$message({
                                message: this.$t("prompt.success"),
                                type: "success",
                                duration: 500,
                                onClose: () => {
                                    this.getDataList();
                                },
                            });
                        })
                }).catch(() => {
                    console.log(1)
                });
        },
    edit(id, type) {
      this.type = type;
      this.addOrUpdateHandle(id);
    },
  },
};
</script>
